/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Beans;

import Entities.TblAcademic;
import Entities.TblStaff;
import Entities.TblStudent;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author CuongNV
 */
@ManagedBean
@RequestScoped
public class StudentManagedBean extends TblStudent {

    /** Creates a new instance of StudentManagedBean */
    public StudentManagedBean() {
    }
    private List<TblStudent> all;
    private static TblStudent studentSelected;
    private int classId;

    public List<TblStudent> getAll() {
        try {
            return new BLL.StudentBLL().getAll();
        } catch (Exception ex) {
            Logger.getLogger(BranchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    //Add student thì chọn class và add luôn TblAcademic
    public String add() {
        int branchId = 0;
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        TblStaff staff = (TblStaff) session.getAttribute("staff");
        branchId = staff.getBranchId();
        try {
            new BLL.StudentBLL().add(this);
            TblAcademic tblAcademic = new TblAcademic();
            tblAcademic.setClassId(classId);
            tblAcademic.setStudentRollNo(this.getStudentRollNo());

            new BLL.StudentBLL().addByBranchId(this, branchId);

            return "listStudent";
        } catch (Exception ex) {
            Logger.getLogger(BranchManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            return "null";
        }
    }

    public String update() throws Exception {
        if (new BLL.StudentBLL().update(studentSelected)) {
            return "academiclist";
        } else {
            return "studentdetail";
        }
    }

    public TblStudent layStudentByRollNo(String studentRollNo) {
        return new BLL.StudentBLL().getById(studentRollNo);
    }

    public TblStudent getStudentSelected() {
        return studentSelected;
    }

    public void setStudentSelected(TblStudent studentSelected) {
        StudentManagedBean.studentSelected = studentSelected;
    }

    public int getClassId() {
        return classId;
    }

    public void setClassId(int classId) {
        this.classId = classId;
    }
}
